; void freichen_asm (unsigned char *src, unsigned char *dst, int h, int w, int row_size)
; r -i asm -f f ../data/base/lena.bmp
global freichen_asm

%define width [ebp - 4]
%define rowsize [ebp - 8]

freichen_asm:

    push ebp
    mov ebp, esp
    sub esp, 8                	    ; 12 bytes para local vars
    push ebx
    push edi
    push esi                	    ; stackframe

    mov dword eax, 2
    movd xmm6, eax
    pshufd xmm6,xmm6, 00000000b
    cvtdq2ps xmm6,xmm6
    sqrtps xmm6, xmm6

    mov esi, [ebp + 8]              ; esi = src
    mov edi, [ebp + 12]             ; edi = dst
    mov ecx, [ebp + 16]             ; ecx = heigh
    mov ebx, [ebp + 20]             ; ebx = width
    mov eax, [ebp + 24]		    ; eax = rowsize

    mov dword width, ebx            ; local width
    mov dword rowsize, eax	    ; local rowsize
    pxor xmm7, xmm7                 ; registro para extender   


    sub ecx, 2			    ; 1er y ultima fila no se procesan

    add esi, ebx		    
    add edi, ebx

ciclo_hfr:
    cmp ecx, 0
    je fin

    xor ebx, ebx
    mov dword ebx, rowsize
    xor edx, edx
    mov edx, width
    sub edx, 1			    ; ebx y edx = rowsize
    inc esi
    inc edi			    ; fila 2 col 2

ciclo_wfr:
    cmp edx, 0                      ; si termine con la fila, voy al ciclo de h
    je fin_fila

    cmp edx, 16
    jbe retrocederfr

sigo_w:
					 ; calculo derivada en x
    xor ebx, ebx
    mov dword ebx, rowsize
    ;**************************************************
    ;    X2  |  X1  |  X0
    ;    Y2  |  Y1  |  Y0
    ;    Z2  |  Z1  |  Z0
    ;**************************************************

    movdqu xmm0, [esi + ebx + 1]	 ; abajo der
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
   
    neg ebx

    movdqu xmm1, [esi + ebx + 1]	 ; arriba der: [esi - ebx + 1]    
    punpcklbw xmm1, xmm7
   punpcklwd xmm1, xmm7
    
   paddd xmm0, xmm1 			 ; 
    
    neg ebx

    cvtdq2ps xmm0,xmm0

    movdqu xmm1, [esi + 1]		 ; medio der
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

  
;Z2 - Z0   = XMM0 
    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi + ebx - 1]	 ; abajo izq
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1			 ; (abajo der - abajo izq)

    neg ebx

    movdqu xmm1, [esi + ebx - 1]	 ; arriba izq 
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1			 ;

    cvtdq2ps xmm0,xmm0

;XMM0  = (Z2 - Z0) + (X2 - X0)    

    neg ebx

    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi - 1]		 ; medio izq
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7     
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0,xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    movdqa xmm5, xmm0	 		 ; guardo resultado de la derivada en x, en xmm5


    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx - 1]	 ; ab izq : [esi + ebx - 1]
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
       
    movdqu xmm1, [esi + ebx + 1]	  ; abajo der
    punpcklbw xmm1, xmm7
 punpcklwd xmm1, xmm7
   
    paddd xmm0, xmm1
    cvtdq2ps xmm0,xmm0
  neg ebx
    
    movdqu xmm1, [esi + ebx]	 	   ; ab medio
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7    
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1
    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    neg ebx    
    ;**************************************************
    ;**************************************************

    neg ebx				   ; ebx negativo

    movdqu xmm1, [esi + ebx - 1]	   ; arriba izq : [esi - ebx - 1]
    punpcklbw xmm1, xmm7
    punpcklwd xmm0, xmm7
   
    psubusw xmm0, xmm1

    movdqu xmm1, [esi + ebx + 1]     	  ; arriba der
    punpcklbw xmm1, xmm7
    punpcklwd xmm0, xmm7
   
    psubd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    
;**************  CALCULO LOS MEDIOS
  

; VEO EL OTRO MEDIO
    movdqu xmm1, [esi + ebx]	    	   ; arriba medio
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0, xmm1
    neg ebx


    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    
    ;**************************************************
    ;  SUMO Y MUEVO A MEMORIA
    ;**************************************************

    paddusb xmm5, xmm0	 	          ; sumo el resultado de la derivada en y, a la derivada en x
   ; PSLLDQ xmm5, 4    

    movd [edi], xmm5

    add esi, 4
    add edi, 4
    sub edx, 4
    jmp ciclo_wfr


retrocederfr:                       
    xor eax, eax
    mov dword eax, 16
  ; mov edx, eax           
    sub eax, edx                   ; (ebx < 16) -> (16 - ebx > 0)
    sub esi, eax                   ; corro el puntero para atras
    sub edi, eax                   ; corro el puntero para atras

    mov dword edx, 16

jmp calculo_ultimos

fin_fila:
    dec ecx
jmp ciclo_hfr
    
fin:
    pop esi                        ; desarmo el stack frame
    pop edi          
    pop ebx
    add esp, 8
    pop ebp
    ret


                                                                     
                                             
calculo_ultimos:
   
    pxor xmm4,xmm4

    ;**************************************************
    ;**************************************************
    ;**************************************************
    ;  CICLO 1
    ;**************************************************
    ;**************************************************
    ;**************************************************
    
    movdqu xmm0, [esi + ebx + 1]	 ; abajo der
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
   
    neg ebx

    movdqu xmm1, [esi + ebx + 1]	 ; arriba der: [esi - ebx + 1]    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7   
    paddd xmm0, xmm1 			 ; 
    
    neg ebx

    cvtdq2ps xmm0,xmm0

    movdqu xmm1, [esi + 1]		 ; medio der
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

 
;Z2 - Z0   = XMM0 
    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi + ebx - 1]	 ; abajo izq
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1			 ; (abajo der - abajo izq)

    neg ebx

    movdqu xmm1, [esi + ebx - 1]	 ; arriba izq 
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1			 ;

    cvtdq2ps xmm0,xmm0

;XMM0  = (Z2 - Z0) + (X2 - X0)    

    neg ebx

    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi - 1]		 ; medio izq
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7     
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0,xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    movdqa xmm5, xmm0	 		 ; guardo resultado de la derivada en x, en xmm5


    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx - 1]	 ; ab izq : [esi + ebx - 1]
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
       
    movdqu xmm1, [esi + ebx + 1]	  ; abajo der
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    paddd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
  neg ebx
    
    movdqu xmm1, [esi + ebx]	 	   ; ab medio
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7    
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1
    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    neg ebx    
    ;**************************************************
    ;**************************************************

    neg ebx				   ; ebx negativo

    movdqu xmm1, [esi + ebx - 1]	   ; arriba izq : [esi - ebx - 1]
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1

    movdqu xmm1, [esi + ebx + 1]     	  ; arriba der
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    
;**************  CALCULO LOS MEDIOS
  

; VEO EL OTRO MEDIO
    movdqu xmm1, [esi + ebx]	    	   ; arriba medio
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0, xmm1
    neg ebx


    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    
    ;**************************************************
    ;  SUMO Y MUEVO A MEMORIA
    ;**************************************************

    paddusb xmm5, xmm0
    PSLLDQ xmm5, 0
    por xmm4, xmm5
    ;**************************************************
    ;**************************************************
    ;**************************************************
    ;  CICLO 2
    ;**************************************************
    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx + 1]	 ; abajo der
    PSRLDQ xmm0, 4    
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
   
    neg ebx

    movdqu xmm1, [esi + ebx + 1]	 ; arriba der: [esi - ebx + 1]    
    PSRLDQ xmm1, 4    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    paddd xmm0, xmm1 			 ; 
    
    neg ebx

    cvtdq2ps xmm0,xmm0

    movdqu xmm1, [esi + 1]		 ; medio der
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3


;Z2 - Z0   = XMM0 
    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi + ebx - 1]	 ; abajo izq
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1			 ; (abajo der - abajo izq)

    neg ebx

    movdqu xmm1, [esi + ebx - 1]	 ; arriba izq 
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1			 ;

    cvtdq2ps xmm0,xmm0

;XMM0  = (Z2 - Z0) + (X2 - X0)    

    neg ebx

    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi - 1]		 ; medio izq
    PSRLDQ xmm1, 4    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7     
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0,xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    movdqa xmm5, xmm0	 		 ; guardo resultado de la derivada en x, en xmm5


    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx - 1]	 ; ab izq : [esi + ebx - 1]
    PSRLDQ xmm0, 4
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
       
    movdqu xmm1, [esi + ebx + 1]	  ; abajo der
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7 
    paddd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
  neg ebx
    
    movdqu xmm1, [esi + ebx]	 	   ; ab medio
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7    
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1
    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    neg ebx    
    ;**************************************************
    ;**************************************************

    neg ebx				   ; ebx negativo

    movdqu xmm1, [esi + ebx - 1]	   ; arriba izq : [esi - ebx - 1]
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1

    movdqu xmm1, [esi + ebx + 1]     	  ; arriba der
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
   
    psubd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    
;**************  CALCULO LOS MEDIOS
  

; VEO EL OTRO MEDIO
    movdqu xmm1, [esi + ebx]	    	   ; arriba medio
    PSRLDQ xmm1, 4
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0, xmm1
    neg ebx


    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    
    ;**************************************************
    ;  SUMO Y MUEVO A MEMORIA
    ;**************************************************

    paddusb xmm5, xmm0
    PSLLDQ xmm5, 4    
    por xmm4, xmm5

        ;**************************************************
    ;**************************************************
    ;**************************************************
    ;  CICLO 3
    ;**************************************************
    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx + 1]	 ; abajo der
    PSRLDQ xmm0, 8    
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7

    neg ebx

    movdqu xmm1, [esi + ebx + 1]	 ; arriba der: [esi - ebx + 1]    
    PSRLDQ xmm1, 8    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    paddd xmm0, xmm1 			 ; 
    
    neg ebx

    cvtdq2ps xmm0,xmm0

    movdqu xmm1, [esi + 1]		 ; medio der
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3


;Z2 - Z0   = XMM0 
    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi + ebx - 1]	 ; abajo izq
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    psubd xmm0, xmm1			 ; (abajo der - abajo izq)

    neg ebx

    movdqu xmm1, [esi + ebx - 1]	 ; arriba izq 
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    psubd xmm0, xmm1			 ;

    cvtdq2ps xmm0,xmm0

;XMM0  = (Z2 - Z0) + (X2 - X0)    

    neg ebx

    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi - 1]		 ; medio izq
    PSRLDQ xmm1, 8    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7     
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0,xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    movdqa xmm5, xmm0	 		 ; guardo resultado de la derivada en x, en xmm5


    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx - 1]	 ; ab izq : [esi + ebx - 1]
    PSRLDQ xmm0, 8
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
    
    movdqu xmm1, [esi + ebx + 1]	  ; abajo der
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    paddd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    neg ebx
    
    movdqu xmm1, [esi + ebx]	 	   ; ab medio
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7    
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1
    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    neg ebx    
    ;**************************************************
    ;**************************************************

    neg ebx				   ; ebx negativo

    movdqu xmm1, [esi + ebx - 1]	   ; arriba izq : [esi - ebx - 1]
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    psubd xmm0, xmm1

    movdqu xmm1, [esi + ebx + 1]     	  ; arriba der
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
 
    psubd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    
;**************  CALCULO LOS MEDIOS
  

; VEO EL OTRO MEDIO
    movdqu xmm1, [esi + ebx]	    	   ; arriba medio
    PSRLDQ xmm1, 8
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0, xmm1
    neg ebx


    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    
    ;**************************************************
    ;  SUMO Y MUEVO A MEMORIA
    ;**************************************************

    paddusb xmm5, xmm0
    PSLLDQ xmm5, 8    
    por xmm4, xmm5

    ;**************************************************
    ;**************************************************
    ;**************************************************
    ;  CICLO 4
    ;**************************************************
    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx + 1]	 ; abajo der
    PSRLDQ xmm0, 12    
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7

    neg ebx

    movdqu xmm1, [esi + ebx + 1]	 ; arriba der: [esi - ebx + 1]    
    PSRLDQ xmm1, 12    
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    paddd xmm0, xmm1 			 ; 
    
    neg ebx

    cvtdq2ps xmm0,xmm0

    movdqu xmm1, [esi + 1]		 ; medio der
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

;Z2 - Z0   = XMM0 
    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi + ebx - 1]	 ; abajo izq
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    psubd xmm0, xmm1			 ; (abajo der - abajo izq)

    neg ebx

    movdqu xmm1, [esi + ebx - 1]	 ; arriba izq 
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1			 ;

    cvtdq2ps xmm0,xmm0

;XMM0  = (Z2 - Z0) + (X2 - X0)    

    neg ebx

    ;**************************************************
    ;**************************************************

    movdqu xmm1, [esi - 1]		 ; medio izq
    PSRLDQ xmm1, 12   
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7     
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0,xmm1    

    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    movdqa xmm5, xmm0	 		 ; guardo resultado de la derivada en x, en xmm5


    ;**************************************************
    ;**************************************************

    movdqu xmm0, [esi + ebx - 1]	 ; ab izq : [esi + ebx - 1]
    PSRLDQ xmm0, 12
    punpcklbw xmm0, xmm7
    punpcklwd xmm0, xmm7
    
    movdqu xmm1, [esi + ebx + 1]	  ; abajo der
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    paddd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
  neg ebx
    
    movdqu xmm1, [esi + ebx]	 	   ; ab medio
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7    
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    addps xmm0, xmm1
    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    neg ebx    
    ;**************************************************
    ;**************************************************

    neg ebx				   ; ebx negativo

    movdqu xmm1, [esi + ebx - 1]	   ; arriba izq : [esi - ebx - 1]
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    psubd xmm0, xmm1

    movdqu xmm1, [esi + ebx + 1]     	  ; arriba der
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7

    psubd xmm0, xmm1

    cvtdq2ps xmm0,xmm0
    
;**************  CALCULO LOS MEDIOS
  

; VEO EL OTRO MEDIO
    movdqu xmm1, [esi + ebx]	    	   ; arriba medio
    PSRLDQ xmm1, 12
    punpcklbw xmm1, xmm7
    punpcklwd xmm1, xmm7
    cvtdq2ps xmm1,xmm1
    mulps xmm1, xmm6

    subps xmm0, xmm1
    neg ebx


    cvtps2dq xmm3, xmm0
    movdqa  xmm0, xmm3

    packssdw xmm0, xmm7
    pabsw xmm0,xmm0
    packuswb xmm0, xmm7
    
    ;**************************************************
    ;  SUMO Y MUEVO A MEMORIA
    ;**************************************************

    paddusb xmm5, xmm0
    PSLLDQ xmm5, 12    
    por xmm4, xmm5

    movdqu [edi], xmm4
    add esi, 16
    add edi, 16
    mov dword edx,0
    

    jmp ciclo_wfr

